import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/exam/dto/assessment.response.dart';
import 'package:ichinesereader/exam/placement/PlaceSelectGroup.dart';

import 'PlaceController.dart';
import 'PlacePlayerWidget.dart';

class PlaceStepTwo extends GetView<PlaceController> {
  const PlaceStepTwo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    String dataJson =
        "{\"question\":[{\"head\":{\"show\":\"text|audio\",\"text\":\"I have learned Chinese for ____ years.\",\"audio\":\"https://ichinsereader-prod.s3.amazonaws.com/content/media/placement/audio/04.mp3\"},\"body\":[[{\"baseValue\":\"A\",\"show\":\"image|text\",\"text\":\"0 - 0.5\",\"image\":\"https://ichinesereader.com/placement/static/media/1A.2f6127f0.PNG\"},{\"baseValue\":\"B\",\"show\":\"image|text\",\"text\":\"0.5 - 2\",\"image\":\"https://ichinesereader.com/placement/static/media/1B.b0c5b6db.PNG\"},{\"baseValue\":\"C\",\"show\":\"image|text\",\"text\":\"2 - 4\",\"image\":\"https://ichinesereader.com/placement/static/media/1C.c9506a88.PNG\"},{\"baseValue\":\"D\",\"show\":\"image|text\",\"text\":\"4 +\",\"image\":\"https://ichinesereader.com/placement/static/media/1D.4bc6e198.PNG\"}]]},{\"head\":{\"show\":\"text|audio\",\"text\":\"My Chinese speaking and listening ability:\",\"audio\":\"https://ichinsereader-prod.s3.amazonaws.com/content/media/placement/audio/06.mp3\"},\"body\":[[{\"baseValue\":\"A\",\"show\":\"image|text|audio\",\"text\":\"Chinese is NOT spoken in my home.\",\"image\":\"https://ichinesereader.com/placement/static/media/2a.c4bbb2b9.PNG\",\"audio\":\"https://ichinsereader-prod.s3.amazonaws.com/content/media/placement/audio/07.mp3\"},{\"baseValue\":\"B\",\"show\":\"image|text|audio\",\"text\":\"I speak a little Chinese with my family.\",\"image\":\"https://ichinesereader.com/placement/static/media/2b.7bc6f1f9.PNG\",\"audio\":\"https://ichinsereader-prod.s3.amazonaws.com/content/media/placement/audio/08.mp3\"},{\"baseValue\":\"C\",\"show\":\"image|text|audio\",\"text\":\"I understand most Chinese conversations about daily life.\",\"image\":\"https://ichinesereader.com/placement/static/media/2c.d2bd1c45.PNG\",\"audio\":\"https://ichinsereader-prod.s3.amazonaws.com/content/media/placement/audio/09.mp3\"},{\"baseValue\":\"D\",\"show\":\"image|text|audio\",\"text\":\"I speak Chinese as well as (or even better than) someone who has grown up in China.\",\"image\":\"https://ichinesereader.com/placement/static/media/2d.c5d56c71.PNG\",\"audio\":\"https://ichinsereader-prod.s3.amazonaws.com/content/media/placement/audio/10.mp3\"}]]},{\"head\":{\"show\":\"text|audio\",\"text\":\"My Chinese writing level :\",\"audio\":\"https://ichinsereader-prod.s3.amazonaws.com/content/media/placement/audio/11.mp3\"},\"body\":[[{\"baseValue\":\"A\",\"show\":\"image|text|audio\",\"text\":\"Not able to write anything in Chinese yet, can only trace.\",\"image\":\"https://ichinesereader.com/placement/static/media/3a.6a69b33e.PNG\",\"audio\":\"https://ichinsereader-prod.s3.amazonaws.com/content/media/placement/audio/12.mp3\"},{\"baseValue\":\"B\",\"show\":\"image|text|audio\",\"text\":\"Able to write some words or simple sentences in Chinese characters or PINYIN.\",\"image\":\"https://ichinesereader.com/placement/static/media/3b.9b3df1bc.PNG\",\"audio\":\"https://ichinsereader-prod.s3.amazonaws.com/content/media/placement/audio/14.mp3\"},{\"baseValue\":\"C\",\"show\":\"image|text|audio\",\"text\":\"Able to write some sentences in Chinese characters or PINYIN.\",\"image\":\"https://ichinesereader.com/placement/static/media/3c.060be0c1.PNG\",\"audio\":\"https://ichinsereader-prod.s3.amazonaws.com/content/media/placement/audio/15.mp3\"},{\"baseValue\":\"D\",\"show\":\"image|text|audio\",\"text\":\"Able to write an essay in Chinese characters to explain a point of view.\",\"image\":\"https://ichinesereader.com/placement/static/media/3d.a6387848.PNG\",\"audio\":\"https://ichinsereader-prod.s3.amazonaws.com/content/media/placement/audio/16.mp3\"}]]},{\"head\":{\"show\":\"text|audio\",\"text\":\"I can read ____.\",\"audio\":\"https://ichinsereader-prod.s3.amazonaws.com/content/media/placement/audio/17.mp3\"},\"body\":[[{\"baseValue\":\"A\",\"show\":\"image|text|audio\",\"text\":\"0 Chinese books.\",\"image\":\"https://ichinesereader.com/placement/static/media/4a.d4c0fd35.PNG\",\"audio\":\"https://ichinsereader-prod.s3.amazonaws.com/content/media/placement/audio/18.mp3\"},{\"baseValue\":\"B\",\"show\":\"image|text|audio\",\"text\":\"Simple Chinese picture books.\",\"image\":\"https://ichinesereader.com/placement/static/media/4b.d57c7f93.PNG\",\"audio\":\"https://ichinsereader-prod.s3.amazonaws.com/content/media/placement/audio/19.mp3\"},{\"baseValue\":\"C\",\"show\":\"image|text|audio\",\"text\":\"A letter written in Chinese.\",\"image\":\"https://ichinesereader.com/placement/static/media/4c.6dc22329.PNG\",\"audio\":\"https://ichinsereader-prod.s3.amazonaws.com/content/media/placement/audio/20.mp3\"}]]}]}";
    QuestionSection section = QuestionSection.fromJson(json.decode(dataJson));
    List<QuestionItem> itemList = section.question!;
    List<Widget> options = [];
    for (int i = 0; i < itemList.length; i++) {
      RxString selectValue = ''.obs;
      QuestionItem item = itemList[i];
      options.add(Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _buildHeader(item.head!),
          SizedBox(height: 10),
          Obx(() {
            return PlaceSelectGroup(
                items: item.body![0],
                value: selectValue.value,
                onChanged: (value) {
                  selectValue.value = value;
                  controller.stepTwoAnswerList.replaceRange(i, i + 1, [value]);
                  // _verifyAnswerNum();
                });
          }),
          SizedBox(height: 10)
        ],
      ));
    }
    return Column(children: options);
  }

  Widget _buildHeader(QuestionContent head) {
    return RichText(
        text: TextSpan(
            text: head.text,
            style: TextStyle(
                fontSize: 18,
                color: Color(0xff212529),
                height: 1.5,
                fontWeight: FontWeight.w800),
            children: [
          WidgetSpan(
              alignment: PlaceholderAlignment.middle,
              child: PlacePlayerWidget(audioUrl: head.audio!))
        ]));
  }
/*
  void _verifyAnswerNum() {
    bool isHave = true;
    for (String str in controller.stepTwoAnswerList) {
      if (str.isEmpty) {
        isHave = false;
        break;
      }
    }
    controller.bottomBtnStatus.value = isHave ? 4 : 3;
  }
 */
}
